// pages/phoneBind/phoneBind.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var md5 = require('../../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    password: '',
    seconds: 0,
    maskLayer: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    // if (app.globalData.accountInfo.audit_status == 2) {
    //   this.failureDetails();
    // }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      audit_status: app.globalData.accountInfo.audit_status
    })
  },
  //注册失败详情
  failureDetails: function() {
    var that = this;
    api.POST({
      url: app.globalData.base_url + "user/regDetails",
      success: function(res) {
        if (res.data.code == '') {
          that.setData({
            text: res.data.data.reason
          })
          wx.showModal({
            title: '审核失败',
            content: that.data.text,
            cancelText: '知道了',
            confirmText: '重新申请',
            success: function(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/center/pages/register/register'
                });
              }
            }
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  //倒计时
  countdown: function() {
    var that = this;
    var timer = setTimeout(function() {
      that.data.seconds--;
      if (that.data.seconds > 0) {
        that.setData({
          codeText: that.data.seconds + "s"
        })
        that.countdown();
      } else {
        that.setData({
          codeText: "获取验证码"
        })
        clearTimeout(timer);
      }
    }, 1000)
  },
  //获取验证码
  getCode: function() {
    var that = this;
    if (this.data.seconds > 0) {
      return false
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      util.showError("手机号不正确");
      return false
    }
    var mobile = that.data.phone
    var type = '23'
    var user_type = '0'
    api.POST({
      params: {
        user_type: user_type,
        mobile: mobile,
        type: type
      },
      url: app.globalData.base_url + "captcha/get",
      success: function(res) {
        if (res.data.code == '') {
          util.showError("发送成功");
          that.setData({
            seconds: 60,
            codeText: '60s'
          })
          that.countdown();
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },

  login: function() {
    // console.log(this.data.phone)
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      util.showError("手机号不正确");
      return false
    }
    if (this.data.password == '') {
      util.showError("请输入密码");
      return false
    }
    var mobile = this.data.phone
		var password = md5(this.data.password)
    api.POST({
      params: {
				password: password,
        mobile: mobile
      },
			url: app.globalData.base_url + "user/binding",
      success: function(res) {
        if (res.data.code == '') {
          app.globalData.accountInfo.user_flag = 2
          app.globalData.accountInfo.inn_id = res.data.data.inn_id
          util.showError("登录成功");
          wx.switchTab({
            url: '/pages/center/center'
          });
        } else {
          util.showError(res.data.message);
        }
      },
			fail:function(e){
				util.showError('网络错误，请检查网络');
			}
    })

  },
  phoneValue: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  passwordValue: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  register: function() {
    wx.navigateTo({
      url: '/center/pages/register/register'
    })
  },

  //不同意协议
  cancel: function() {
		wx.navigateBack({
			delta:1
		})
  },

  //同意协议
  certain: function() {
    this.setData({
      maskLayer: true,
    })
  },
})